package com.guigu.tms.dispatch.dto;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.guigu.tms.commons.aggregation.entity.TransportLine;
import com.guigu.tms.commons.dispatch.entity.CacheLine;
import com.guigu.tms.commons.dispatch.entity.CacheLineDetail;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.*;

@Data
public class MachiningRoutePlanningDTO {

    private Map<Integer, List<CacheLineDetail>> lineMap = new HashMap<>();
    private Map<String, TransportLine> transportLineMap = new HashMap<>();


    //其实机构编号
    private String startAgencyId;

    //目的机构编号
    private String endAgencyId;

    //可达线路原型
    private RoutePlanningDTO routePlanningDTO;

    /**
     * 加工可用线路 返回完整格式
     * @return
     */
    public Map<CacheLine, List<CacheLineDetail>> get(){
        HashMap<CacheLine, List<CacheLineDetail>> cacheLineListHashMap = new HashMap<>();
        for (List<CacheLineDetail> value : lineMap.values()) {
            Collections.reverse(value);
            String cacheLineId= IdWorker.getIdStr();//路线编号
            BigDecimal distance=new BigDecimal(0);//距离
            BigDecimal cost=new BigDecimal(0);//价格
            BigDecimal time=new BigDecimal(0);//时间
            for (int i = 0; i < value.size(); i++) {
                CacheLineDetail cacheLineDetail = value.get(i);
                cacheLineDetail.setCacheLineId(cacheLineId);
                cacheLineDetail.setSort(i+1);
                TransportLine line = transportLineMap.get(cacheLineDetail.getCacheLineId());
                distance.add(line.getDistance());
                cost.add(line.getCost());
                time.add(line.getEstimatedTime());
            }
            CacheLine cacheLine = new CacheLine();
            cacheLine.setId(cacheLineId);
            cacheLine.setDistance(distance);
            cacheLine.setCost(cost);
            cacheLine.setEstimatedTime(time.toString());
            cacheLine.setVerifyKey("");
            cacheLine.setStartAgencyId(this.getStartAgencyId());
            cacheLine.setEndAgencyId(this.getEndAgencyId());
            cacheLine.setIsCurrent(true);
            cacheLine.setTransferCount(value.size());
            cacheLineListHashMap.put(cacheLine,value);
        }

        return cacheLineListHashMap;
    }

    public MachiningRoutePlanningDTO(String startAgencyId,String endAgencyId,RoutePlanningDTO routePlanningDTO){
        this.startAgencyId=startAgencyId;
        this.endAgencyId=endAgencyId;
        this.routePlanningDTO=routePlanningDTO;
        build(routePlanningDTO);
    }

    public List<Integer> build(RoutePlanningDTO routePlanningDTO){
        ArrayList<Integer> indexList = new ArrayList<>();
        //当前机构可达其他机构线路
        List<TransportLine> transferTransportLine = routePlanningDTO.getTransferTransportLine();
        if(!CollectionUtils.isEmpty(transferTransportLine)) {
            for (int i = 0; i < transferTransportLine.size(); i++) {
                TransportLine item = transferTransportLine.get(i);
                List<Integer> indexList1=build(routePlanningDTO.getRoutePlanningDTOMap().get(item.getId()));
                if(indexList1.size()>0) {
                    // 含有可行线路，增加上级中转点
                    for (Integer index : indexList1) {
                        CacheLineDetail cacheLineDetail = new CacheLineDetail();
                        cacheLineDetail.setStartAgencyId(item.getStartAgencyId());
                        cacheLineDetail.setEndAgencyId(item.getEndAgencyId());
                        cacheLineDetail.setTransportLineId(item.getId());
                        lineMap.get(index).add(cacheLineDetail);
                        transportLineMap.put(item.getId(),item);
                        indexList.add(index);
                    }
                }
            }
        }
        //直达线路
        List<TransportLine> directTransportLine = routePlanningDTO.getDirectTransportLine();
        if(!CollectionUtils.isEmpty(directTransportLine)){
            for (int i = 0; i < directTransportLine.size(); i++) {
                TransportLine item=directTransportLine.get(i);
                CacheLineDetail cacheLineDetail=new CacheLineDetail();
                cacheLineDetail.setStartAgencyId(item.getStartAgencyId());
                cacheLineDetail.setEndAgencyId(item.getEndAgencyId());
                cacheLineDetail.setTransportLineId(item.getId());
                ArrayList<CacheLineDetail> cacheLineDetails = new ArrayList<>();
                cacheLineDetails.add(cacheLineDetail);
                lineMap.put(++i,cacheLineDetails);
                indexList.add(i);
                transportLineMap.put(item.getId(),item);
            }
        }
        return indexList;
    }



}
